package service

import (
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func GetIpInfo(ip string) (res string, err error) {
	if len(ip) == 0 {
		return "", errors.New("请输入IP地址")
	}
	log.Println("请求ip", ip)
	url := fmt.Sprintf("http://ip-api.com/json/%s?lang=zh-CN", ip)
	r, err := http.Get(url)
	if err != nil {
		return "", err
	}
	defer r.Body.Close()
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return "", err
	}
	return string(b), nil
}